package ar.edu.unlu.sistemas.p2p.business.msg;

import org.jdom.Element;

import ar.edu.unlu.sistemas.p2p.business.util.Params;

public class Header {
	
	public static final String TAG_ROOT = "header";
	private static final String TAG_MSG_ID = "id";
	public static final String TAG_MSG_TYPE = "type";
	private static final String TAG_MSG_TTL = "ttl";
	private static final String TAG_MSG_HOP = "hops";
	
	private String msgId;
	private int type;
	private int ttl = Params.MSG_TTL;
	private int hops = 0;
	
	private Header(){
	}
	
	public Header(String msgId,int type){
		this.setMsgId(msgId);
		this.setType(type);
	}
	
	public Header(int type){
		this.setType(type);
	}
	
	public String getMsgId() {
		return msgId;
	}
	public void setMsgId(String msgId) {
		this.msgId = msgId;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public int getTtl() {
		return ttl;
	}
	public void setTtl(int ttl) {
		this.ttl = ttl;
	}
	public int getHops() {
		return hops;
	}
	public void setHops(int hops) {
		this.hops = hops;
	}
	
	public Element toXMLElement() {
		Element el = new Element(TAG_ROOT);
		el.addContent(new Element(TAG_MSG_ID).setText(this.getMsgId()));
		el.addContent(new Element(TAG_MSG_TYPE).setText(String.valueOf(this.getType())));
		el.addContent(new Element(TAG_MSG_TTL).setText(String.valueOf(this.getTtl())));
		el.addContent(new Element(TAG_MSG_HOP).setText(String.valueOf(this.getHops())));
		return el;
	}
	
	public static Header buildFromXMLElement(Element headerElement) {
		Header header =new Header();
		header.setMsgId(headerElement.getChild(TAG_MSG_ID).getText());
		header.setType(Integer.parseInt(headerElement.getChild(TAG_MSG_TYPE).getText()));
		header.setTtl(Integer.parseInt(headerElement.getChild(TAG_MSG_TTL).getText()));
		header.setHops(Integer.parseInt(headerElement.getChild(TAG_MSG_HOP).getText()));
		return header;
	}

}
